package com.xiaomaoguai.cloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.xiaomaoguai.cloud.dao.UserRepository;
import com.xiaomaoguai.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;


/**
 * 用户控制器
 *
 * @author weihui
 * @date 2020/01/20
 */
@RestController
public class UserController {

	/**
	 * 用户存储库
	 */
	@Autowired
	private UserRepository userRepository;

	/**
	 * 发现客户端
	 */
	@Autowired
	private EurekaClient discoveryClient;

	/**
	 * 获取用户
	 *
	 * @param id id
	 * @return {@link Optional<User>}
	 */
	@GetMapping("/simple/{id}")
	public Optional<User> getUser(@PathVariable Long id) {
		return userRepository.findById(id);
	}

	/**
	 * 服务Url
	 *
	 * @return {@link InstanceInfo}
	 */
	@GetMapping("/eureka-instance")
	public InstanceInfo serviceUrl() {
		return discoveryClient.getNextServerFromEureka("microservice-provider-user", false);
	}

}
